<?php
/**
 * 货品Logic
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2017/12/29
 * Time: 18:21
 */

namespace App\Http\Logics\MiniProgramAdmin;


use App\Goods;
use App\Product;
use App\ProductContent;
use App\ProductMappingTag;
use App\ProductPhoto;
use App\Http\Logics\BaseLogic;
use App\Http\Logics\CommonLogic;
use Illuminate\Support\Facades\DB;

class ProductLogic extends BaseLogic
{
    public static function createProduct($data)
    {
        DB::transaction(function () use ($data) {
            // 处理货品图片
            $photo_list_arr = json_decode($data['photo_list_str']);

            $response = self::getUserIdByLoginSession($data['loginSession']);
            if ($response->code) {
                return self::response($response->code);
            }
            $user_id = $response->data;
            $product = new Product();
            $product->cover = $photo_list_arr[0]->url;
            $product->name = $data['name'];
            $product->user_id = $user_id;
            $product->partner_id = $data['partner_id'];
            $product->cafe_id = $data['cafe_id'];
            $product->category_id = $data['category_id'];
            $product->carriage = $data['carriage'];
            if (!$product->save()) {
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            $product_id = $product->id;

            // 处理货品图片
            foreach ($photo_list_arr as $key => $item) {
                $url = $item->url . "?imageInfo";
                // 处理图片
                $rs = dealHttp($url);
                $width = $rs->width;
                $height = $rs->height;
                $product_photo = new ProductPhoto();
                $product_photo->product_id = $product_id;
                $product_photo->url = $item->url;
                $product_photo->sort = $key;
                $product_photo->width = $width;
                $product_photo->height = $height;
                if (!$product_photo->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }

            // 处理货品商品
            $standard_list_arr = json_decode($data['standard_list_str']);
            foreach ($standard_list_arr as $item) {
                $goods = new Goods();
                $goods->no = strtoupper('NO'.date('Y').date('m').date('d').date('h').date('i').date('s').randomStr(6));
                $goods->standard = $item->standard;
                $goods->num = $item->num;
                $goods->price = $item->price;
                $goods->icon = $item->icon;
                $goods->product_id = $product_id;
                if (!$goods->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }

            // 货品图文详情
            if (isset($data['content_str']) && $data['content_str'] != '') {
                $content = json_decode($data['content_str']);
                foreach ($content as $key => $item) {
                    $product_content = new ProductContent();
                    // 公众参数
                    $product_content->product_id = $product_id;
                    $product_content->sort = $key;
                    $response = CommonLogic::handleContent($product_content, $item);
                    if ($response->code) {
                        return self::response($response->code, $response->data);
                    }
                }
            }

            // 处理货品标签
            if (isset($data['tag_list_str']) && $data['tag_list_str'] != '') {
                $tag_list_arr = json_decode($data['tag_list_str']);
                foreach ($tag_list_arr as $key => $item) {
                    $tag_arr = explode( '-', $item);
                    foreach ($tag_arr as $value) {
                        $product_mapping_tag = new ProductMappingTag();
                        $product_mapping_tag->product_id = $product_id;
                        $product_mapping_tag->tag_id = $value;
                        if (!$product_mapping_tag->save()) {
                            return self::response(self::OBJECT_SAVE_FAIL);
                        }
                    }
                }
            }
        });

        return self::response(self::SUCCESS);
    }

    public static function getProductList($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        $query = Product::where(function($query) use ($user_id, $data){
            $query->where('user_id', $user_id);
            if (isset($data['cafe_id']) && $data['cafe_id']){
                $query->where('cafe_id', $data['cafe_id']);
            }
            if (isset($data['partner_id']) && $data['partner_id']){
                $query->where('partner_id', $data['partner_id']);
            }
        })->orderBy('id', 'desc');
        $product = $data['limit'] ? $query->paginate($data['limit']) : $query->get();
        $product->map(function ($product) {
            $max_price = Goods::where('product_id', $product->id)->max('price');
            $min_price = Goods::where('product_id', $product->id)->min('price');
            $product->max_price = $max_price / 100;
            $product->min_price = $min_price / 100;
            return $product;
        });
        if (empty($product)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $product);
    }

    public static function getProductDetail($data)
    {
        $product = Product::with(['productContent', 'category', 'tag', 'goods', 'productPhoto'])->find($data['product_id']);
        if (empty($product)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $product);
    }

    public static function changeStatus($data)
    {
        $product = Product::find($data['product_id']);
        if (empty($product)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        $product->status = !$product->status;
        if (!$product->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS, $product->status);
    }

    public static function deleteProduct($data)
    {
        $product = Product::find($data['product_id']);
        if (empty($product)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        if (!$product->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function updateProduct($data)
    {
        DB::transaction(function () use ($data) {
            // 处理货品图片
            $photo_list_arr = json_decode($data['photo_list_str']);

            $response = self::getUserIdByLoginSession($data['loginSession']);
            if ($response->code) {
                return self::response($response->code);
            }
            $user_id = $response->data;
            $product = Product::find($data['product_id']);
            $product->cover = $photo_list_arr[0]->url;
            $product->name = $data['name'];
            $product->user_id = $user_id;
            $product->category_id = $data['category_id'];
            $product->carriage = $data['carriage'];
            if (!$product->save()) {
                return self::response(self::OBJECT_SAVE_FAIL);
            }
            $product_id = $product->id;

            // 处理货品图片
            ProductPhoto::where('product_id', $product_id)->delete();
            foreach ($photo_list_arr as $key => $item) {
                $url = $item->url . "?imageInfo";
                // 处理图片
                $rs = dealHttp($url);
                $width = $rs->width;
                $height = $rs->height;
                $product_photo = new ProductPhoto();
                $product_photo->product_id = $product_id;
                $product_photo->url = $item->url;
                $product_photo->sort = $key;
                $product_photo->width = $width;
                $product_photo->height = $height;
                if (!$product_photo->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }

            // 处理货品商品
            $standard_list_arr = json_decode($data['standard_list_str']);
            Goods::where('product_id', $product_id)->whereNotIn('id', array_column($standard_list_arr, 'id'))->delete();
            foreach ($standard_list_arr as $item) {
                $goods = isset($item->id) ? Goods::find($item->id) : new Goods();
                $goods->no = strtoupper('NO'.date('Y').date('m').date('d').date('h').date('i').date('s').randomStr(6));
                $goods->standard = $item->standard;
                $goods->num = $item->num;
                $goods->price = $item->price;
                $goods->icon = $item->icon;
                $goods->product_id = $product_id;
                if (!$goods->save()) {
                    return self::response(self::OBJECT_SAVE_FAIL);
                }
            }

            // 货品图文详情
            $content = json_decode($data['content_str']);
            ProductContent::where('product_id', $product_id)->whereNotIn('id', array_column($content, 'id'))->delete();
            if (isset($data['content_str']) && $data['content_str'] != '') {
                $content = json_decode($data['content_str']);
                foreach ($content as $key => $item) {
                    $product_content = isset($item->id) ? ProductContent::find($item->id) : new ProductContent();
                    // 公众参数
                    $product_content->product_id = $product_id;
                    $product_content->sort = $key;
                    $response = CommonLogic::handleContent($product_content, $item);
                    if ($response->code) {
                        return self::response($response->code, $response->data);
                    }
                }
            }

            // 处理货品标签
            ProductMappingTag::where('product_id', $product_id)->delete();
            if (isset($data['tag_list_str']) && $data['tag_list_str'] != '') {
                $tag_list_arr = json_decode($data['tag_list_str']);
                foreach ($tag_list_arr as $key => $item) {
                    $tag_arr = explode( '-', $item);
                    foreach ($tag_arr as $value) {
                        $product_mapping_tag = new ProductMappingTag();
                        $product_mapping_tag->product_id = $product_id;
                        $product_mapping_tag->tag_id = $value;
                        if (!$product_mapping_tag->save()) {
                            return self::response(self::OBJECT_SAVE_FAIL);
                        }
                    }
                }
            }
        });

        return self::response(self::SUCCESS);
    }
}